package com.hcb.model.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.hcb.common.ActionEvent;
import com.hcb.constants.Constants;
import com.hcb.controller.ContactController;
import com.hcb.dto.ContactsDTO;
import com.hcb.dto.CustomerDTO;
import com.hcb.fragment.EmergencyContactsFragment;

public class ContactModel {
	public static ContactModel instance;

	public static ContactModel getInstance() {
		if (instance == null)
			instance = new ContactModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {

		switch (e.action) {
		case Constants.GET_CONTACT:
			ArrayList<ContactsDTO> arrData = getContact((Object) e.sender);
			e.viewData = arrData;
			ContactController.getInstance().handleModelViewEvent(e);
			break;
		case Constants.INSERT_CONTACT:
			ContactsDTO dtois = (ContactsDTO) e.viewData;
			insertContact(dtois, (Object) e.sender);
			break;
		case Constants.INSERT_CONTACT_EXITS:
			ContactsDTO dtoexit = (ContactsDTO) e.viewData;
			insertContactExit(dtoexit, (Context) e.sender);
			break;

		case Constants.UPDATE_CONTACT:
			ContactsDTO dtoup = (ContactsDTO) e.viewData;
			updateContact(dtoup, (Object) e.sender);
			break;
		case Constants.DELETE_CONTACT:
			ContactsDTO dtodl = (ContactsDTO) e.viewData;
			deleteContact(dtodl, (Context) e.sender);
			break;

		default:
			break;
		}
	}

	public ArrayList<ContactsDTO> getContact(Object ob) {
		EmergencyContactsFragment ab = (EmergencyContactsFragment) ob;
		Context ctx = ab.getActivity().getApplicationContext();
		ArrayList<ContactsDTO> arr = new ArrayList<ContactsDTO>();
		String sql = "SELECT * FROM contact";
		DBHelper db = new DBHelper(ctx);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			ContactsDTO con = new ContactsDTO();
			con.setId(c.getInt(c.getColumnIndex("id")));
			con.setName(c.getString(c.getColumnIndex("namephone")));
			con.setNumber(c.getString(c.getColumnIndex("numberphone")));
			arr.add(con);
		}
		c.close();
		db.close();
		return arr;
	}

	public boolean checkContact(ContactsDTO dto, Context context) {
		boolean flag = false;
		String sql = "SELECT * FROM contact where namephone = '"
				+ dto.getName() + "' and numberphone = '" + dto.getNumber()
				+ "'";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertContact(ContactsDTO dto, Object ob) {
		boolean flag = false;
		EmergencyContactsFragment ab = (EmergencyContactsFragment) ob;
		Context ctx = ab.getActivity().getApplicationContext();
		boolean check = checkContact(dto, ctx);
		if (!check) {
			String DATABASE_TABLE = Constants.TABLE_CONTACT;
			ContentValues values = new ContentValues();

			values.put("namephone", dto.getName());
			values.put("numberphone", dto.getNumber());
			DBHelper db = DBHelper.getInstance(ctx);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;

	}
	public boolean insertContactExit(ContactsDTO dto, Context context) {
		boolean flag = false;
		boolean check = checkContact(dto, context);
		if (!check) {
			String DATABASE_TABLE = Constants.TABLE_CONTACT;
			ContentValues values = new ContentValues();

			values.put("namephone", dto.getName());
			values.put("numberphone", dto.getNumber());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;

	}

	public boolean updateContact(ContactsDTO dto, Object ob) {
		EmergencyContactsFragment ab = (EmergencyContactsFragment) ob;
		Context ctx = ab.getActivity().getApplicationContext();
		boolean flag = false;
		String DATABASE_TABLE = Constants.TABLE_CONTACT;
		String where = "id= " + dto.getId();
		ContentValues values = new ContentValues();

		values.put("namephone", dto.getName());
		values.put("numberphone", dto.getNumber());
		DBHelper db = DBHelper.getInstance(ctx);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;

	}

	public void deleteContact(ContactsDTO dto, Context context) {
		String sql = "namephone = '" + dto.getName() + "' and numberphone = '"
				+ dto.getNumber() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TABLE_CONTACT;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();

	}
}
